package com.lizi.core.RPC;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import com.caucho.hessian.io.HessianInput;
import com.caucho.hessian.io.HessianOutput;
import com.lizi.domian.UserBean;

/**
 * 对象序列化反序列化
 * 
 * @author Administrator
 *
 */
public class ObjectSerializers {

	

	public static void main(String[] args) throws Exception {
		UserBean user = new UserBean();
		user.setName("namasde");

		jdk(user);
		hessian(user);
	}
	
	public static void jdk(UserBean user) throws Exception {
		// 定义一个字节数组输出流
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		// 对象输出流
		ObjectOutputStream out = new ObjectOutputStream(os);
		// 将对象写入到字节数组输出，进行序列化
		out.writeObject(user);
		byte[] userByte = os.toByteArray();

		// 反序列化
		ByteArrayInputStream is = new ByteArrayInputStream(userByte);
		ObjectInputStream in = new ObjectInputStream(is);
		UserBean userNew = (UserBean) in.readObject();

		System.out.println(userNew.getName());
	}
	
	public static void hessian(UserBean user) throws Exception {
		// 定义一个字节数组输出流
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		// 对象序列化输出
		HessianOutput ho = new HessianOutput(os);
		// 将对象写入到字节数组输出，进行序列化
		ho.writeObject(user);
		byte[] userByte = os.toByteArray();

		// 反序列化
		ByteArrayInputStream is = new ByteArrayInputStream(userByte);
		// 对象反序列化
		HessianInput in = new HessianInput(is);
		UserBean userNew = (UserBean) in.readObject();

		System.out.println(userNew.getName());
	}
	
}
